package com.hjy.product.web;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hjy.common.utils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/test1")
    public R test1() {
        // entry 方法中指定资源的名称
        try (Entry entry = SphU.entry("resourceName-test1")) {
            System.out.println("执行业务逻辑成功");
        } catch (BlockException ex) {
            // 资源访问阻止，被限流或被降级
            // 在此处进行相应的处理操作
            System.out.println("被流控或熔断, 执行回调");
        }
        return R.ok().put("data", "hello world");
    }

    @GetMapping("/test2")
    public R test2() {
        return hello3();
    }

    @SentinelResource(value = "resourceName-test3", blockHandler = "blockHandlerForGetUser")
    public R hello3() {
        return R.ok().put("hehhe", "xixixi");
    }

    // blockHandler 函数，原方法调用被限流/降级/系统保护的时候调用
    public R blockHandlerForGetUser() {
        return R.error();
    }
}
